package com.baidu.health.service.impl;

import com.baidu.health.mapper.TbUserMapper;
import com.baidu.health.pojo.ResultResponse;
import com.baidu.health.pojo.TbUser;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.baidu.health.pojo.TbMerchantAuddit;
import com.baidu.health.service.TbMerchantAudditService;
import com.baidu.health.mapper.TbMerchantAudditMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 *
 */
@Service
public class TbMerchantAudditServiceImpl extends ServiceImpl<TbMerchantAudditMapper, TbMerchantAuddit>
    implements TbMerchantAudditService{
    @Autowired
    private TbMerchantAudditMapper tbMerchantAudditMapper;
    @Autowired
    private TbUserMapper tbUserMapper;
    @Override
    public ResultResponse userMerchant(TbMerchantAuddit merchantAuddit) {
        QueryWrapper queryWrapper=new QueryWrapper();
        queryWrapper.eq("user_id",merchantAuddit.getUserId());
        TbMerchantAuddit tbMerchantAuddit = tbMerchantAudditMapper.selectOne(queryWrapper);
        if(tbMerchantAuddit!=null){
            return ResultResponse.FAILED(500,"该用户已经申请称为商家，请勿重复注册");
        }
        QueryWrapper queryWrapper1=new QueryWrapper();
        queryWrapper1.eq("merchant_name",merchantAuddit.getMerchantName());
        TbMerchantAuddit tbMerchantAuddit1 = tbMerchantAudditMapper.selectOne(queryWrapper1);
        if(tbMerchantAuddit1!=null){
            return ResultResponse.FAILED(500,"该商店名字已被使用，请勿重复");
        }
        merchantAuddit.setMerchantStatus(0);
        merchantAuddit.setCreateTime(new Date());
        tbMerchantAudditMapper.insert(merchantAuddit);
        return ResultResponse.SUCCESS("已经审核，请耐心等待");
    }

    @Override
    public ResultResponse userMerchantid(Integer merchantId, Integer userId, Integer status) {
        TbMerchantAuddit tbMerchantAuddit=new TbMerchantAuddit();
        tbMerchantAuddit.setMerchantId(merchantId);
        tbMerchantAuddit.setUserId(userId);
        tbMerchantAuddit.setMerchantStatus(status);
        tbMerchantAudditMapper.updateById(tbMerchantAuddit);
        if(tbMerchantAuddit.getUserId()==1){
            TbUser tbUser=new TbUser();
            tbUser.setUserId(userId);
            tbUser.setUserRole((long) 1);
            tbUserMapper.updateById(tbUser);
        }
        return ResultResponse.SUCCESS();
    }

    @Override
    public ResultResponse Merchantrevoke(Integer userid) {
        tbMerchantAudditMapper.deleteById(userid);
        return ResultResponse.SUCCESS();
    }

    @Override
    public ResultResponse MerchantSelect() {
        List<TbMerchantAuddit> list = tbMerchantAudditMapper.selectList(null);
        return ResultResponse.SUCCESS(list);
    }
}




